package com.example.jsonarry.nanningtravel;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.*;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.example.jsonarry.nanningtravel.Utils.Utils;
import com.example.jsonarry.nanningtravel.adapter.DividerGridViewItemDecoration;
import com.example.jsonarry.nanningtravel.adapter.MyStaggedRecyclerAdapter;
import com.example.jsonarry.nanningtravel.adapter.NavigationRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

public class NavigationActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener
{

    @Bind(R.id.fab) FloatingActionButton fab;
    @Bind(R.id.nav_view) NavigationView navView;
    @Bind(R.id.drawer_layout) DrawerLayout drawerLayout;
    @Bind(R.id.recyclerView) RecyclerView recyclerview;
    @Bind(R.id.toolbar) Toolbar toolbar;
    private Snackbar snackbar;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);

//        CollapsingToolbarLayout collapsingToolbar =(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
//        collapsingToolbar.setTitle("Title");
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                snackbar = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG);
                snackbar.setAction("Action", new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Toast.makeText(NavigationActivity.this, "onclick", Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();

        navView.setNavigationItemSelectedListener(this);

        initData();
        //设置线性布局管理器--垂直、水平
//		recyclerview.setLayoutManager(new LinearLayoutManager(this));
//		recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true));
//		recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
//        adapter = new MyStaggedRecyclerAdapter(this, list);
        MyStaggedRecyclerAdapter adapter = new MyStaggedRecyclerAdapter(this, list);
        recyclerview.setAdapter(adapter);

        recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerview.addItemDecoration(new DividerGridViewItemDecoration(this,0));
        //设置条目点击事件
//        adapter.setOnItemClickListener(new MyStaggedRecyclerAdapter.OnItemClickListener()
//        {
//
//            @Override
//            public void onItemClick(View view, int position)
//            {
//                Toast.makeText(getApplicationContext(), "条目" + position + "被点击", Toast.LENGTH_SHORT).show();
//            }
//        });
//        adapter.setOnItemClickLongListener(new MyStaggedRecyclerAdapter.OnItemLongClickListener()
//        {
//
//            @Override
//            public void onItemLongClick(View view, int position)
//            {
//                Toast.makeText(getApplicationContext(), "条目" + position + "被长按~~", Toast.LENGTH_SHORT).show();
//                adapter.deleteData(position);
//            }
//        });
    }

    private void initData()
    {
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++)
        {
            list.add(i + "crystal");
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (snackbar != null)
        {
            snackbar.dismiss();
        }
        return true;
    }

    @Override
    public void onBackPressed()
    {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START))
        {
            drawer.closeDrawer(GravityCompat.START);
        }
        else
        {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            //com.tencent.mm.ui.tools.AddFavoriteUI
            // com.tencent.mm.ui.tools.ShareImgUI
            //com.tencent.mobileqq.activity.qfileJumpActivity
            //cooperation.qqfav.widget.QfavJumpActivity
            //com.tencent.mobileqq.activity.JumpActivity
            Utils.shareText(this, new String[]{
                   /* "com.tencent.mm.ui.tools.ShareImgUI"*/
                    "com.tencent.mm"
                    , "com.tencent.mobileqq.activity.JumpActivity"
                    , "mms"});
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camara)
        {
            // Handle the camera action

        }
        else if (id == R.id.nav_gallery)
        {

        }
        else if (id == R.id.nav_slideshow)
        {

        }
        else if (id == R.id.nav_manage)
        {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        else if (id == R.id.nav_share)
        {

        }
        else if (id == R.id.nav_send)
        {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
